package com.kaifamiao.wendao.dao;

import com.kaifamiao.wendao.entity.Customer;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class CustomerDao extends BaseDao<Customer,Long> {

    private static final String INSERT_ONE="INSERT INTO t_customers (id,username,nickname,password,salt,reg_date) VALUES (?,?,?,?,?,?)";

    private static final String UPDATE_ONE="UPDATE t_customers SET username=?, password=?,  nickname=? WHERE id =?";
    private static final String REMOVE_ONE="DELETE FROM t_customers WHERE id=?";
    private static final String SELECT_ALL="SELECT id, username, password, salt, nickname, reg_date FROM t_customers ORDER BY id";
    private static final String SELECT_ONE_BY_ID="SELECT id, username, password, salt, nickname, reg_date FROM t_customers WHERE id=?";
    private static final String SELECT_ONE_BY_UNAME="SELECT id, username, password, salt, nickname, reg_date  FROM t_customers WHERE username=?";

    private Customer wrap(ResultSet rs)throws SQLException {
        Customer c = new Customer();
        c.setId( rs.getLong( "id" ) );
        c.setUsername( rs.getString( "username" ) );
        c.setPassword( rs.getString( "password" ) );
        c.setSalt( rs.getString( "salt" ) );
        c.setNickname( rs.getString( "nickname" ) );
        java.sql.Date date = rs.getDate( "reg_date" );
        LocalDate regDate = date == null ? null : date.toLocalDate();
        c.setRegisterDate( regDate );
        return c;
    }


    @Override
    public boolean save(Customer customer) {
        LocalDate date=customer.getRegisterDate();
        java.sql.Date reg_date=date!=null?java.sql.Date.valueOf(date):null;
        Object[] params={customer.getId(),customer.getUsername(),customer.getNickname(),customer.getPassword(),customer.getSalt(),reg_date};
        try{
            return runner.update(INSERT_ONE,params)==1;
        }catch (SQLException e){
            throw new RuntimeException("保存失败",e);
        }
    }

    @Override
    public boolean update(Customer customer) {
        Object[] params = {customer.getUsername(),customer.getPassword(),customer.getNickname(), customer.getId()};
        try{
            return runner.update(UPDATE_ONE,params)==1;
        }catch (SQLException e){
            throw new RuntimeException("更新用户信息失败",e);
        }
    }

    @Override
    public boolean remove(Long id) {
        try{
            return runner.update(REMOVE_ONE,id)==1;
        }catch (SQLException e){
            throw new RuntimeException("删除失败",e);
        }
    }

    @Override
    public List<Customer> findAll() {
        ResultSetHandler<List<Customer>> rsHandler =resultSet -> {
            List<Customer> list=new ArrayList<>();
            while (resultSet.next()){
                Customer customer=wrap(resultSet);
                list.add(customer);
            }
            return list;
        };
        try{
            return runner.query(SELECT_ALL,rsHandler);
        }catch (SQLException e){
            throw new RuntimeException("查询所有用户信息失败");
        }
    }

    @Override
    public Customer find(Long id) {
        ResultSetHandler<Customer> rsHandler = rs -> {
            if( rs.next() ) {
                Customer c = wrap(rs);
                return c ;
            }
            return null;
        };
        try {
            return runner.query(SELECT_ONE_BY_ID, rsHandler, id );
        } catch (SQLException cause) {
            throw new RuntimeException("查询失败", cause);
        }
    }

    public Customer find(String username) {
        ResultSetHandler<Customer> rsHandler = rs -> {
            if( rs.next() ) {
                Customer c = wrap(rs);
                return c ;
            }
            return null;
        };
        try {
            return runner.query(SELECT_ONE_BY_UNAME, rsHandler, username );
        } catch (SQLException cause) {
            throw new RuntimeException("查询失败", cause);
        }
    }


}
